﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAEKAUDIT : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        decimal RelesedQty
        {
            get
            {
                if (txtReleased.Text.ToString().Trim() == "")
                    txtReleased.Text = "0";
                return Convert.ToDecimal(txtReleased.Text.ToString());
            }
            set
            {
                txtReleased.Text = value.ToString();
            }
        }
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public YCAEKAUDITData m_Data, m_DataX;
        public DataSet dsParamaters;
        public YCAEKAUDITReport m_rpt;
        bool IsSupplierLoded = false;
        public string PrnType = "EXCEL";
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }
        public SETINTEXDB Set4ITX
        {
            get
            {
                return (SETINTEXDB)TryFindResource("Set4ITX");
            }
        }

        #region=====vwLIEFDAT=======
        CollectionViewSource vwLIEFDAT
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwLIEFDAT");
            }
        }
        DataRow dwLIEFDAT
        {
            get
            {
                if (vwLIEFDAT.View == null)
                    return null;
                if ((DataRowView)vwLIEFDAT.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwLIEFDAT.View.CurrentItem).Row;
            }
        }
        #endregion

        #region=====vwEKKOPF=======
        CollectionViewSource vwEKKOPF
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKKOPF");
            }
        }
        DataRow dwEKKOPF
        {
            get
            {
                if (vwEKKOPF.View == null)
                    return null;
                if ((DataRowView)vwEKKOPF.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKKOPF.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwEKPOS=======
        CollectionViewSource vwEKPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwEKPOS");
            }
        }
        DataRow dwEKPOS
        {
            get
            {
                if (vwEKPOS.View == null)
                    return null;
                if ((DataRowView)vwEKPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwEKPOS.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwABAUFM=======
        CollectionViewSource vwABAUFM
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABAUFM");
            }
        }
        DataRow dwABAUFM
        {
            get
            {
                if (vwABAUFM.View == null)
                    return null;
                if ((DataRowView)vwABAUFM.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABAUFM.View.CurrentItem).Row;
            }
        }
        #endregion

        public string KRS
        {
            get
            {
                return m_Data.Right("  " + txtEKPAUFKRS.Text.ToString().Trim(), 2);
            }
            set
            {
                txtEKPAUFKRS.Text = value;
            }
        }
        public string LFD
        {
            get
            {
                return m_Data.Right("000000" + txtEKPAUFLFD.Text.ToString().Trim(), 6); ;
            }
            set
            {
                txtEKPAUFLFD.Text = value;
            }
        }
        public string POS
        {
            get
            {
                return m_Data.Right("000000" + txtEKPAUFPOS.Text.ToString().Trim(), 3); ;
            }
            set
            {
                txtEKPAUFPOS.Text = value;
            }
        }
        BindingListCollectionView cvABA
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgABA.ItemsSource);
            }
        }
        #endregion
        public YCAEKAUDIT()
        {
            InitializeComponent();
            this.Name = "采购单管理";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = false;
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];

            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            m_Data = new YCAEKAUDITData(OracleDB, proxy);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {

                if (!IsSupplierLoded)
                {
                    SetData.Merge(m_Data.LoadSupplier());
                    IsSupplierLoded = true;
                }

                vwEKKOPF.View.CurrentChanged += vwEKKOPF_CurrentChanged;
                vwLIEFDAT.View.CurrentChanged += vwLIEFDAT_CurrentChanged;
                vwEKPOS.View.CurrentChanged += vwEKPOS_CurrentChanged;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }

        void vwEKPOS_CurrentChanged(object sender, EventArgs e)
        {
            if (dwEKPOS == null)
            {
                cvABA.CustomFilter = null;
                return;
            }              
            string filter = "ABAAUFKRS='" + dwEKPOS["EKPAUFKRS"].ToString() + "' AND ABAAUFLFD='" + dwEKPOS["EKPAUFLFD"].ToString() + "' AND ABAAUFPOS='" + dwEKPOS["EKPAUFPOS"].ToString() + "'";
            cvABA.CustomFilter = filter;
        }

        void vwLIEFDAT_CurrentChanged(object sender, EventArgs e)
        {
            if (dwLIEFDAT == null)
                return;

            DataSet Result = new DataSet();
            Result = m_Data.GetOrderBySupplier(dwLIEFDAT);
            SetData.EKKOPF.Clear();
            SetData.Merge(Result, false, MissingSchemaAction.Add);
        }

        void vwEKKOPF_CurrentChanged(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            dwInput = dwEKKOPF;
            if (dwInput == null)
            {
                return;
            }
            Result = m_Data.BuildData(dwInput);
            Set4ITX.Clear();
            Set4ITX.Merge(Result, false, MissingSchemaAction.Add);
        }
        public void btnQuery_Click(object sender, EventArgs e)
        {
            DataSet Result = new DataSet();
            DataRow dwInput;
            dwInput = dwEKKOPF;
            try
            {
                if (dwInput == null)
                {
                    if (LFD.Trim() == "0000000")
                        return;
                    dwInput = SetData.EKPOS.NewRow();
                    dwInput["EKPMANDANT"] = "00";
                    dwInput["EKPAUFKRS"] = KRS;
                    dwInput["EKPAUFLFD"] = LFD;
                }
                RelesedQty = 0;
                Result = m_Data.BuildData(dwInput);
                Set4ITX.Clear();
                Set4ITX.Merge(Result);
                if (vwEKPOS.View != null)
                {
                    vwEKPOS.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

   
        public void btnApprove_Click(object sender, EventArgs e)
        {
            try
            {
                if (dwEKPOS == null)
                    return;
                DataRow dwData = SetData.EKPOS.NewRow();
                dwData["EKPMANDANT"] = dwEKPOS["EKPMANDANT"];
                dwData["EKPAUFKRS"] = dwEKPOS["EKPAUFKRS"];
                dwData["EKPAUFLFD"] = dwEKPOS["EKPAUFLFD"];
                dwData["EKPAUFPOS"] = dwEKPOS["EKPAUFPOS"];
                dwData["EKPBEKZ"] = "N";
                dwData["EKPKNTRNRL"] = m_Data.day + "." + m_Data.month + "." + Helpers.Helper.Right(m_Data.year, 2) + "       ";
                m_Data.INTEXDBC.UpdateEKPOS(dwData, 4);
                btnQuery_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        

        private void cmdBack2EK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwEKPOS == null)
                    return;
                DataRow dwData = SetData.EKPOS.NewRow();
                dwData["EKPMANDANT"] = dwEKPOS["EKPMANDANT"];
                dwData["EKPAUFKRS"] = dwEKPOS["EKPAUFKRS"];
                dwData["EKPAUFLFD"] = dwEKPOS["EKPAUFLFD"];
                dwData["EKPAUFPOS"] = dwEKPOS["EKPAUFPOS"];
                dwData["EKPBEKZ"] = "N";
                dwData["EKPKNTRNRL"] = "               ";
                m_Data.INTEXDBC.UpdateEKPOS(dwData, 4);
                btnQuery_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void cmdBack2EKDispo_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwEKPOS == null)
                    return;
                DataRow dwData = SetData.EKPOS.NewRow();
                dwData["EKPMANDANT"] = dwEKPOS["EKPMANDANT"];
                dwData["EKPAUFKRS"] = dwEKPOS["EKPAUFKRS"];
                dwData["EKPAUFLFD"] = dwEKPOS["EKPAUFLFD"];
                dwData["EKPAUFPOS"] = dwEKPOS["EKPAUFPOS"];
                dwData["EKPBEKZ"] = " ";
                dwData["EKPKNTRNRL"] = "               ";
                m_Data.INTEXDBC.UpdateEKPOS(dwData, 4);
                btnQuery_Click(null, null);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }


        private void cmdDeleteSingleReq_Click(object sender, RoutedEventArgs e)
        {
            string filter = "";
            DataRow[] rows;
            try
            {
                if (dwEKPOS == null)
                {
                    MessageBox.Show("请选择采购单!");
                    return;
                }

                if (dwABAUFM == null)
                {
                    MessageBox.Show("请定位需求!");
                    return;
                }
                string krs, lfd, pos,abaid;
                krs = dwABAUFM["ABAAUFKRS"].ToString();
                lfd = dwABAUFM["ABAAUFLFD"].ToString();
                pos = dwABAUFM["ABAAUFPOS"].ToString();
                abaid = dwABAUFM["ABAID"].ToString();
                decimal EKPME1 = Convert.ToDecimal(dwEKPOS["EKPME1"].ToString()) / 100;
                var q1 = from DataRow rx in Set4ITX.ICC_ABAUFM.AsEnumerable()
                         where rx["ABAID"].ToString() == abaid
                         select rx;
                foreach (DataRow row in q1)
                {
                    row["ABAAUFTYP"] = "N";
                    row["ABAAUFKRS"] = "  ";
                    row["ABAAUFLFD"] = "      ";
                    row["ABAAUFPOS"] = "   ";
                    row["ABAQTYRESERVE"] = 0;
                    RelesedQty += Convert.ToDecimal(row["ABARESMG"].ToString());       
                    m_Data.INTEXDBC.UpdateICC_ABAUFM(row, 3);
                } 
                //filter = "ABAID='" + dwABAUFM["ABAID"].ToString() + "'";
                //rows = Set4ITX.ICC_ABAUFM.Select(filter);
                decimal QTYREQ = 0;
                var q2 = from DataRow rx in Set4ITX.ICC_ABAUFM.AsEnumerable()
                         where rx["ABAAUFKRS"].ToString() == krs
                         && rx["ABAAUFLFD"].ToString() ==lfd
                         && rx["ABAAUFPOS"].ToString() == pos
                         select rx;
             
                foreach (DataRow row in q2)
                {                 
                    QTYREQ += Convert.ToDecimal(row["ABARESMG"].ToString());                   
                }
                foreach (DataRow row in q2)
                {
                    decimal req=Convert.ToDecimal(row["ABARESMG"].ToString());
                    row["ABAQTYRESERVE"] = Math.Round((req / QTYREQ) * EKPME1);
                    m_Data.INTEXDBC.UpdateICC_ABAUFM(row, 3);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }


        }
        private void cmdNewReq_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwEKPOS == null)
                {
                    MessageBox.Show("请寻找一条采购单行!");
                    return;
                }
                if(dwEKPOS["EKPBEKZ"].ToString().Trim()=="R")
                {
                    MessageBox.Show("已经补采购，不要重复操作!");
                    return;
                }
                dwEKPOS["EKPBEKZ"] = "R";
                string filter = "ABAAUFKRS='" + dwEKPOS["EKPAUFKRS"].ToString() + "' AND ABAAUFLFD='" + dwEKPOS["EKPAUFLFD"].ToString() + "' AND ABAAUFPOS='" + dwEKPOS["EKPAUFPOS"].ToString() + "'";
                DataRow[] rows = Set4ITX.ICC_ABAUFM.Select(filter);
                foreach (DataRow row in rows)
                {
                    row["ABAID"] = 0;                    
                    row["ABAAUFTYP"] = "N";
                    row["ABAAUFKRS"] = "  ";
                    row["ABAAUFLFD"] = "      ";
                    row["ABAAUFPOS"] = "   ";
//                    row["ABARESMG"] = 0;
                    row["ABAQTYRESERVE"] = 0;
                    row["ABAQTYRECEIVED"] = row["ABARESMG"];
                    /*foreach (DataColumn CM in row.Table.Columns)
                    {
                        if (row[CM.ColumnName].ToString().Trim() == "")
                        {
                            if (CM.DataType.ToString().ToLower().Contains("string"))
                            {
                                row[CM.ColumnName] = " ";
                            }
                            else
                                row[CM.ColumnName] =0;                            
                        }
                    }*/
                    m_Data.INTEXDBC.InsertICC_ABAUFM(row,9);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void cmdDeleteEK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (txtDelete.Text.ToString().Trim() == "N")
                {
                    MessageBox.Show("如果你确认删除，请将标志改为Y");
                    return;
                }

                listGS08N.Text = "";
                string client = "00";
                string krs = m_Data.Right("  " + txtEKPAUFKRS.Text.ToString().Trim(), 2);
                string lfd = m_Data.Right("000000" + txtEKPAUFLFD.Text.ToString().Trim(), 6);
                string pos = m_Data.Right("000" + txtEKPAUFPOS.Text.ToString().Trim(), 3);
                m_Data.Delete(client, krs, lfd, pos);
                txtDelete.Text = "N";
                listGS08N.Text = krs + lfd + "已经成功删除！";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

    }
}
